﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AI_Experiment;


namespace AI_Experiment
{
    public partial class Droids_Mainform : Form
    {
        public Droids_Mainform()
        {
            InitializeComponent();
        }

        private void AI_MainForm_Load(object sender, EventArgs e)
        {
            //set everything to random by default, later possibly add save settings option

            if (userRandDim.Checked == true)
            {
                //disables and clears input
                userDefDimXInput.Enabled = false;
                userDefDimXInput.Text = "";
                userDefDimYInput.Enabled = false;
                userDefDimYInput.Text = "";
            }

            if (userRandNumDroids.Checked == true)
            {
                userNumDroidsInput.Enabled = false;
            }

            if (userRandNumFoods.Checked == true)
            {
                userNumFoodsInput.Enabled = false;
            }
        }

        private void userDefDim_CheckedChanged(object sender, EventArgs e)
        {
            //flip input boxes to enabled or disabled depending on if random is clicked or not
            if (userRandDim.Checked == false)
            {
                userDefDimXInput.Enabled = true;
                userDefDimYInput.Enabled = true;
            }
            else
            {
                userDefDimXInput.Enabled = false;
                userDefDimYInput.Enabled = false;
            }
        }

        private void userDefNumDroids_CheckedChanged(object sender, EventArgs e)
        {
            //flip input boxes to enabled or disabled depending on if random is clicked or not
            if (userRandNumDroids.Checked == false)
            {
                userNumDroidsInput.Enabled = true;
            }
            else
            {
                userNumDroidsInput.Enabled = false;
            }
        }

        private void userDefNumFoods_CheckedChanged(object sender, EventArgs e)
        {
            //flip input boxes to enabled or disabled depending on if random is clicked or not
            if (userRandNumFoods.Checked == false)
            {
                userNumFoodsInput.Enabled = true;
            }
            else
            {
                userNumFoodsInput.Enabled = false;
            }
        }


        private void genMapButton_Click(object sender, EventArgs e)
        {

            if (userRandDim.Checked == true)
            {
                //GlobalVars.boardSize_x = new Random.Next(0, 100);
                //GlobalVars.boardSize_y = new Random.Next(0, 100);
                GlobalVars.boardSize_x = GlobalVars.random.Next(1, 101);
                GlobalVars.boardSize_y = GlobalVars.random.Next(1, 101);
            }
            else if (userDefDimXInput.Text != "" && userDefDimYInput.Text != "")
            {
                GlobalVars.boardSize_x = int.Parse(userDefDimXInput.Text);
                GlobalVars.boardSize_y = int.Parse(userDefDimYInput.Text);
            }
            Console.WriteLine("-------------------");
            Console.WriteLine("boardSize_x = " + GlobalVars.boardSize_x);
            Console.WriteLine("boardSize_y = " + GlobalVars.boardSize_y);

            //still needs map generation method
        }

        private void spawnDroidsButton_Click(object sender, EventArgs e)
        {
            //clear off the totalDroids counter variable
            GlobalVars.totalDroids = 0;

            //spawn droids
            if (userRandNumDroids.Checked == true)
            {
                //randomize numDroids here
                GlobalVars.numDroids = GlobalVars.random.Next(1, 11);
            }
            else if (userNumDroidsInput.Text != "")
            {
                GlobalVars.numDroids = int.Parse(userNumDroidsInput.Text);
            }
            else
            {
                MessageBox.Show("Please enter a number for the number of droids!");
            }

            //temporary spawning stuff here
            //GlobalVars.totalDroids = GlobalVars.numDroids;
            if (GlobalVars.boardSize_x == 0 || GlobalVars.boardSize_y == 0)
            {
                MessageBox.Show("Please generate the map first!");
            }
            else
            {
                Control.SpawnDroids();
            }
            

            //debug shit
            Console.WriteLine("-------------------");
            Console.WriteLine("numDroids = " + GlobalVars.numDroids);
            Console.WriteLine("totalDroids = " + GlobalVars.totalDroids);
        }

        private void spawnFoodButton_Click(object sender, EventArgs e)
        {
            //spawn foods
            if (userRandNumFoods.Checked == true)
            {
                GlobalVars.numFoods = GlobalVars.random.Next(1, 11);
            }
            else if (userNumFoodsInput.Text != "")
            {
                GlobalVars.numFoods = int.Parse(userNumFoodsInput.Text);
            }
            else
            {
                MessageBox.Show("Please enter a number for the number of food");
            }

            if (GlobalVars.boardSize_x == 0 || GlobalVars.boardSize_y == 0)
            {
                MessageBox.Show("Please generate the map first!");
            }
            else 
            {
                Control.SpawnFood();
            }

            //debug
            Console.WriteLine("-------------------");
            Console.WriteLine("numFoods = " + GlobalVars.numFoods);
            Console.WriteLine("totalFoods = " + GlobalVars.totalFoods);
        }

        private void runSimButton_Click(object sender, EventArgs e)
        {
            //run simulation if droids and food are spawned, if not prompt user
            //spawning droids(aka setting "totalDroids") now and spawning food
            //is completely left up to the respectful buttons in this case except
            //for the "do everything" button
            if (GlobalVars.boardSize_x == 0 || GlobalVars.boardSize_y == 0)
            {
                MessageBox.Show("The board has not been created or has a value of 0 for one dimension", "Droids", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else if (GlobalVars.totalDroids == 0 || GlobalVars.totalFoods == 0)
            {
                MessageBox.Show("Either the Droids or Food have not been spawned", "Droids", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                Control.runSim();
            }

                //debug shit
                Console.WriteLine("-------------------");
                Console.WriteLine("boardSize_x = " + GlobalVars.boardSize_x);
                Console.WriteLine("boardSize_y = " + GlobalVars.boardSize_y);
                Console.WriteLine("totalDroids = " + GlobalVars.totalDroids);
                Console.WriteLine("totalFoods = " + GlobalVars.totalFoods);

        }

        private void doEverythingButton_Click(object sender, EventArgs e)
        {
            //generate map, droids, food and run simulation based on options
                //deal with the size of the map
            if (userRandDim.Checked == true)
            {
                GlobalVars.boardSize_x = GlobalVars.random.Next(1, 101);
                GlobalVars.boardSize_y = GlobalVars.random.Next(1, 101);
                //needs map generation methods here
            }
            else
            {
                GlobalVars.boardSize_x = int.Parse(userDefDimXInput.Text);
                GlobalVars.boardSize_y = int.Parse(userDefDimYInput.Text);
                //needs map generation methods here
            }
            //deal with droids
            if (userRandNumDroids.Checked == true)
            {
                GlobalVars.numDroids = GlobalVars.random.Next(1, 11);

                //this is temporary until droids are actually spawned
                //"spawn" the droids:
                GlobalVars.totalDroids = GlobalVars.numDroids;
            } else if (userDefNumDroids.Checked == true){
                GlobalVars.numDroids = int.Parse(userNumDroidsInput.Text);

                //this is temporary until droids are actually spawned
                //"spawn" the droids:
                GlobalVars.totalDroids = GlobalVars.numDroids;
            }
            //deal with food
            if (userRandNumFoods.Checked == true)
            {
                GlobalVars.numFoods = GlobalVars.random.Next(1, 11);

                //this is temporary until food is actually spawned
                //"spawn" the food:
                GlobalVars.totalFoods = GlobalVars.numFoods;
            }
            else if (userDefNumFoods.Checked == true)
            {
                GlobalVars.numDroids = int.Parse(userNumFoodsInput.Text);

                //this is temporary until food is actually spawned
                //"spawn" the food:
                GlobalVars.totalFoods = GlobalVars.numFoods;
            }
            //debug shit
            Console.WriteLine("-------------------");
            Console.WriteLine("boardSize_x = " + GlobalVars.boardSize_x);
            Console.WriteLine("boardSize_y = " + GlobalVars.boardSize_y);
            Console.WriteLine("numDroids = " + GlobalVars.numDroids);
            Console.WriteLine("totalDroids =" + GlobalVars.totalDroids);
            Console.WriteLine("numFoods = " + GlobalVars.numFoods);
            Console.WriteLine("totalFoods =" + GlobalVars.totalFoods);

            //still needs map generation and spawn execution methods, as well as run simulation method
        }

        private void clearEverythignBtn_Click(object sender, EventArgs e)
        {
            //should this clear the map size? or just wipe off the map?
            GlobalVars.numDroids = 0;
            GlobalVars.numFoods = 0;
            GlobalVars.totalDroids = 0;
            GlobalVars.totalFoods = 0;

            Console.WriteLine("Cleared everything except map size:");
            Console.WriteLine("-------------------");
            Console.WriteLine("boardSize_x = " + GlobalVars.boardSize_x);
            Console.WriteLine("boardSize_y = " + GlobalVars.boardSize_y);
            Console.WriteLine("numDroids = " + GlobalVars.numDroids);
            Console.WriteLine("totalDroids =" + GlobalVars.totalDroids);
            Console.WriteLine("numFoods = " + GlobalVars.numFoods);
            Console.WriteLine("totalFoods =" + GlobalVars.totalFoods);
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}